package com.hupu.jrs.ui.pmlist

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.bean.Pm
import com.hupu.jrs.ui.BaseFragment
import com.hupu.jrs.ui.messagelist.MessageComponent
import com.hupu.jrs.ui.pmdetail.PmDetailActivity
import com.hupu.jrs.widget.LoadMoreRecyclerView
import com.yalantis.phoenix.PullToRefreshView
import javax.inject.Inject

/**
 * Created by sll on 2016/5/6.
 */
class PmListFragment : BaseFragment(), PmListContract.View, PullToRefreshView.OnRefreshListener, LoadMoreRecyclerView.LoadMoreListener, PmListAdapter.OnItemClickListener {

    @BindView(R.id.recyclerView)
    internal var recyclerView: LoadMoreRecyclerView? = null
    @BindView(R.id.refreshLayout)
    internal var refreshLayout: PullToRefreshView? = null

    @Inject
    internal var mPresenter: PmListPresenter? = null
    @Inject
    internal var mAdapter: PmListAdapter? = null

    override fun initInjector() {
        getComponent(MessageComponent::class.java).inject(this)
    }

    override fun initContentView(): Int {
        return R.layout.base_phonix_list_layout
    }

    override fun getBundle(bundle: Bundle) {

    }

    override fun initUI(view: View) {
        ButterKnife.bind(this, view)
        mPresenter!!.attachView(this)
        val layoutManager = LinearLayoutManager(getActivity().getApplicationContext())
        recyclerView!!.setLayoutManager(layoutManager)
        recyclerView!!.setAdapter(mAdapter)
        recyclerView!!.setLoadMoreListener(this)
        refreshLayout!!.setOnRefreshListener(this)
        mAdapter!!.setOnItemClickListener(this)
    }

    override fun initData() {
        mPresenter!!.onPmListReceive()
    }

    override fun showLoading() {
        showProgress(true)
    }

    override fun hideLoading() {
        showContent(true)
    }

    override fun renderPmList(pms: MutableList<Pm>) {
        mAdapter!!.bind(pms)
        recyclerView!!.getAdapter().notifyDataSetChanged()
    }

    override fun onRefreshCompleted() {
        refreshLayout!!.setRefreshing(false)
    }

    override fun onLoadCompleted(haMore: Boolean) {
        recyclerView!!.onLoadCompleted(haMore)
    }

    override fun onError() {
        showError(true)
    }

    override fun onEmpty() {
        setEmptyText("暂无历史消息")
        showEmpty(true)
    }

    override fun showPmDetailUi(uid: String, name: String) {
        PmDetailActivity.startActivity(getActivity(), uid, name)
    }

    override fun onLoadMore() {
        mPresenter!!.onLoadMore()
    }

    override fun onRefresh() {
        mPresenter!!.onRefresh()
    }

    override fun onReloadClicked() {
        mPresenter!!.onReload()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    override fun onPmListClick(pm: Pm?) {
        mPresenter!!.onPmListClick(pm)
    }
}
